package com.example.newinfinity.UserRoom

import android.content.Context
import androidx.lifecycle.LiveData

//Room数据库访问仓库 充当中介 不让外部直接获取Database数据库
class Repository(context: Context) {
    private val userDatabase = UserDatabase.getUserDatabase(context)
    private val userDao = userDatabase.getUserDao()

    suspend fun insertUser(user: User) {
        userDao.insertUser(user)
    }

    suspend fun updateUser(user: User) {
        userDao.updateUser(user)
    }

    //获取当前登录的用户
    suspend fun getCurrentLoginInUser(): User? {
        return userDao.getLoginInUser()
    }

    //根据用户输入的信息判断该用户是否注册过
    suspend fun getUserByName(name: String, password: String): User? {
        return userDao.getUserByName(name, password)
    }

    //获取所有注册过的用户
    fun getAllUsers(): LiveData<List<User>>? {
        return userDao.getAllUsers()
    }
}